/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package plures.transaction;

import java.util.ArrayList;
import persistence.PersistList;
import br.ufu.sdg12014.base.replycoordinator.Transacao;
import br.ufu.sdg12014.base.replycoordinator.Operacao;

/**
 *
 * @author Guilherme
 */
public class Transaction extends PersistList<Command>
{
    private int                 clientKey;
    private ArrayList<Command>  cmds;

    public Transaction(int clientKey, ArrayList<Command> cmds)
    {
        super("trans_" + clientKey);
        this.clientKey = clientKey;
        this.cmds = cmds;
    }  

    public Transaction(int clientKey)
    {
        super("trans_" + clientKey);
        this.clientKey = clientKey;
        this.cmds = new ArrayList<>();
    }

    public Transaction(int client, Transacao t)
    {
        super("trans_" + client);
        this.clientKey = client;
        this.cmds = new ArrayList<>();
        Command c;
        for(Operacao op : t.ops)
        {
            c = new Command(op);
            this.cmds.add(c);
        }
    }
    
    public Transacao toTransacao()
    {
        Transacao transacao = new Transacao();
        transacao.clientToken = this.clientKey;
        transacao.ops = new ArrayList<>();
        for(Command c : this.cmds)
        {
            transacao.ops.add(c.toOperacao());
        }
        return transacao;
    }
    
    public ArrayList<Command> getCmds()
    {
        return cmds;
    }
    
    public boolean add(Command c)
    {
        return this.cmds.add(c);
    }
}
